﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
//inner
using Jc.MicroDesigner.Management;
using Jc.MicroDesigner.Objects;

namespace Jc.MicroDesigner.Tests
{
    /// <summary>
    /// Автоматизированный запуск тестов
    /// </summary>
    public class TestFramework
    {
        List<TestCase> _Tests = null;
        string _appPath = "";

        /// <summary>
        /// Создать тестовую площадку
        /// </summary>
        /// <param name="tests"></param>
        /// <param name="AppPath"></param>
        public TestFramework(List<TestCase> tests, string AppPath)
        {
            _Tests = tests;
            _appPath = AppPath;
        }

        /// <summary>
        /// Запуск всех тестов
        /// </summary>
        /// <param name="resultfolder">Папка для выгрузки результатов</param>
        /// <returns>Список интервалов выполнения</returns>
        public List<TimeSpan> Start()
        {
            string resultfolder = Directory.CreateDirectory(Path.Combine(_appPath, "TestDir")).FullName;
            List<TimeSpan> _Times = new List<TimeSpan>();
            if (_Tests != null)
            {
                Drawings.SignalsPanel panel = new Jc.MicroDesigner.Drawings.SignalsPanel();
                panel.TimeTableVisible = true;
                panel.TimeTagsVisible = true;
                panel.TimeInterval = 5;

                for (int i = 0; i < _Tests.Count; i++)
                {
                    ObjectManager tst = new ObjectManager();
                    tst.AppPath = _appPath;

                    DateTime start = DateTime.Now;
                    List<SignalContainer> data = _Tests[i].TestIt(tst);
                    TimeSpan t = DateTime.Now - start;
                    _Times.Add(t);
                    string file = Path.Combine(resultfolder, _Tests[i].TestName + ".png");
                    panel.GenerateDiagram(data);
                    panel.SaveToImage(file);
                }
            }
            return _Times;
        }
    }
}